package com.foxmail.aroundme.boring.module.weixinDetail;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.R;
import com.foxmail.aroundme.boring.annotation.ActivityFragmentInject;
import com.foxmail.aroundme.boring.base.ui.BaseActivity;
import com.foxmail.aroundme.boring.bean.entry.WeiXinEntry;
import com.foxmail.aroundme.boring.manager.UIHelper;
import com.foxmail.aroundme.boring.utils.Config;
import com.foxmail.aroundme.boring.utils.m_enum.RefreshType;
import com.foxmail.aroundme.boring.weight.GWebView;
import com.socks.library.KLog;

/**
 * Created by gzl on 2/10/17.
 * <p>
 * 微信精选界面
 */
@ActivityFragmentInject(contentViewId = R.layout.activity_web_detail,
        hasTitleBar = true,
        leftButtonId = R.drawable.icon_cancel)
public class WebDetailActivity extends BaseActivity {

    private GWebView gWebView;
    private ProgressBar progressBar;

    @Override
    protected void initView() {
        gWebView = (GWebView) findViewById(R.id.weixin_webview);
        UIHelper.initWebViewDetail(gWebView);
        gWebView.setWebViewClient(new BaWebViewClient());
        gWebView.setWebChromeClient(new BaWebChromeClient());
        progressBar = (ProgressBar) findViewById(R.id.pb_progress);
    }

    @Override
    protected void initIntentData() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }

        String url = intent.getStringExtra(Config.IntentURL);
        String title = intent.getStringExtra(Config.IntentTitle);

        gWebView.loadUrl(url);

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void showProgress(@RefreshType.Refresh int type) {

    }

    @Override
    public void hideProgress() {

    }

    private class BaWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (BuildConfig.DEBUG) {
                KLog.i("shouldOverrideUrlLoading==" + url);
            }
            return super.shouldOverrideUrlLoading(view, url);
        }


        @Override
        public void onPageFinished(WebView view, String url) {
            if (progressBar != null) {
                progressBar.setVisibility(View.GONE);
            }
            super.onPageFinished(view, url);
        }
    }

    private class BaWebChromeClient extends WebChromeClient {
        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            boolean consumed = super.onConsoleMessage(consoleMessage);
            // 输出js console打印的日志
            if (BuildConfig.DEBUG && !consumed) {
                KLog.i("webLog", consoleMessage.message());
            }
            return consumed;
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (BuildConfig.DEBUG) {
                KLog.i(Config.TAG, "newProgress = " + newProgress);
            }
            if (progressBar != null) {
                // 加载中,取消放在上面onPageFinished
                progressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

    }
}
